/* eslint-disable */
import { hiprint } from 'vue-plugin-hiprint';
import { getMainFields, getOtherFields } from './print.api';

const createProvider = function (key, options, title) {
  const addElementTypes = function (context) {
    //context.removePrintElementTypes(key);
    let printElementGroup = new hiprint.PrintElementTypeGroup(title, options);
    context.addPrintElementTypes(key, [printElementGroup]);
  };
  return {
    addElementTypes: addElementTypes,
  };
};


const createProviderList = function (key, optionList) {

  var addElementTypes = function (context) {
    context.removePrintElementTypes(key);
    let printElementTypeGroup = [];
    optionList.map((item) => {
        printElementTypeGroup.push(new hiprint.PrintElementTypeGroup(item.title, item.fields));
    });
    context.addPrintElementTypes(key, printElementTypeGroup);
  };
  return {
    addElementTypes: addElementTypes,
  };
};

export const myProvider = function (params) {
  var addElementTypes = async function (context) {
    let mainFields = await getMainFields(params);
    let otherFields = await getOtherFields(params);
    console.log('params.businessCode:', params.businessCode);
    context.removePrintElementTypes(params.businessCode);
    let printElementTypeGroup = [];
    if (mainFields) {
      printElementTypeGroup.push(new hiprint.PrintElementTypeGroup('平台', mainFields));
    }
    if (otherFields) {
      printElementTypeGroup.push(new hiprint.PrintElementTypeGroup('其他', otherFields));
    }
    context.addPrintElementTypes(params.businessCode, printElementTypeGroup);
  };
  return {
    addElementTypes: addElementTypes,
  };
};

export default { createProvider, createProviderList, myProvider };
